/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.kasse;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import model.EnhedsSalg;
import utility.DoubleRestrictionDocument;
import utility.ImageToolbox;
import view.MainGUI;
import view.touchability.SkærmNumpad;

/**
 *
 * @author Jeppe
 */
public class RabatPopUpPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;
    private final KassePanel kassePanel;
    private EnhedsSalg enhedsSalg;

    /**
     * Creates new form RabatPopUpPanel
     *
     * @param kassePanel
     * @param enhedsSalg
     */
    public RabatPopUpPanel(MainGUI mainGUI, KassePanel kassePanel) {
        initComponents();
        this.mainGUI = mainGUI;
        this.kassePanel = kassePanel;
    }

    public void setPanel(ImageIcon imgIcon, EnhedsSalg enhedsSalg) {
        this.enhedsSalg = enhedsSalg;
        baggrundLabel.setIcon(imgIcon);
        rabatTxt.setText(enhedsSalg.getRabat() + "");
        rabatTxt.selectAll();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        RabatPanel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        rabatTxt = new javax.swing.JTextField();
        panelBaggrund = new javax.swing.JLabel();
        baggrundKnap = new javax.swing.JButton(){
            public void paint (Graphics g){
                if(baggrundKnap.getIcon() != null){
                    ImageToolbox.drawImage(g, (ImageIcon) baggrundKnap.getIcon(), 0, 0, baggrundKnap.getSize().width, baggrundKnap.getSize().height);
                }else{
                    baggrundKnap.setBackground(Color.RED);
                }
            }
        };
        baggrundLabel = new javax.swing.JLabel(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)baggrundLabel.getIcon(), 0, 0, baggrundLabel.getWidth(), baggrundLabel.getHeight());
            }
        };

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                formComponentHidden(evt);
            }
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        RabatPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        RabatPanel.setOpaque(false);
        RabatPanel.setLayout(null);

        jButton1.setText("OK!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        RabatPanel.add(jButton1);
        jButton1.setBounds(190, 30, 90, 54);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("%");
        RabatPanel.add(jLabel1);
        jLabel1.setBounds(130, 37, 40, 40);

        rabatTxt.setDocument(new DoubleRestrictionDocument(3,rabatTxt));
        rabatTxt.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        rabatTxt.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        rabatTxt.setText("0");
        rabatTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                rabatTxtKeyReleased(evt);
            }
        });
        RabatPanel.add(rabatTxt);
        rabatTxt.setBounds(10, 30, 160, 54);

        panelBaggrund.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/betalingsPanelOverlay.png"))); // NOI18N
        RabatPanel.add(panelBaggrund);
        panelBaggrund.setBounds(0, 0, 290, 110);

        add(RabatPanel);
        RabatPanel.setBounds(70, 200, 290, 110);

        baggrundKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrundOverlay.png"))); // NOI18N
        baggrundKnap.setContentAreaFilled(false);
        baggrundKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baggrundKnapActionPerformed(evt);
            }
        });
        add(baggrundKnap);
        baggrundKnap.setBounds(0, 0, 800, 610);
        add(baggrundLabel);
        baggrundLabel.setBounds(0, 0, 800, 610);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!rabatTxt.getText().equals("")) {
            enhedsSalg.setRabat(Integer.parseInt(rabatTxt.getText()));
            kassePanel.opdaterKasseListeOgAktivtSalg();
            mainGUI.showPage("KassePanel");
            JTextField jtf = new JTextField();
            rabatTxt.setBorder(jtf.getBorder());
        }else{
            rabatTxt.setBorder(BorderFactory.createLineBorder(Color.RED));
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void rabatTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_rabatTxtKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            enhedsSalg.setRabat(Integer.parseInt(rabatTxt.getText()));
            kassePanel.opdaterKasseListeOgAktivtSalg();
            mainGUI.showPage("KassePanel");
        }
    }//GEN-LAST:event_rabatTxtKeyReleased

    private void baggrundKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baggrundKnapActionPerformed
        mainGUI.showPage("KassePanel");
    }//GEN-LAST:event_baggrundKnapActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        SkærmNumpad.getInstance().setVisible(true);
        rabatTxt.requestFocus();
        rabatTxt.selectAll();
    }//GEN-LAST:event_formComponentShown

    private void formComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentHidden
        SkærmNumpad.getInstance().setVisible(false);
    }//GEN-LAST:event_formComponentHidden


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel RabatPanel;
    private javax.swing.JButton baggrundKnap;
    private javax.swing.JLabel baggrundLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel panelBaggrund;
    private javax.swing.JTextField rabatTxt;
    // End of variables declaration//GEN-END:variables
}
